package com.yc.hrd.ui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.hrd.core.Item;
import com.yc.hrd.core.Map;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;

public class GameWin {

	protected Shell shell;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			GameWin window = new GameWin();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 469);
		shell.setText("SWT Application");
		RowLayout rl_shell = new RowLayout(SWT.HORIZONTAL);
		rl_shell.center = true;
		rl_shell.justify = true;
		rl_shell.fill = true;
		shell.setLayout(rl_shell);
		
		//Composite composite = new Composite(shell, SWT.NONE);
		composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new RowData(388, 554));
		
		// 构建地图
		buildMap();
		
		shell.layout();
		// 自动计算窗体和控件的大小
		shell.pack();
		
		shell.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(clickItem == null) {
					return;
				}
				// w 上, s 下, a 左, d右边
				if(e.keyCode=='s') {
					map.print();
					if(map.down(clickItem)) {
						map.print();
						buildMap();
					}
				}
			}
		});

	}
	
	private Map map = new Map();
	private Composite composite;
	private static final int SIZE = 100;
	// 记录当前被点击的板块
	private Item clickItem;
	
	public void buildMap() {
		Item[] items = map.getItems();
		
		// 清空容器内部的元素
		for(Control c : composite.getChildren()) {
			c.dispose();
		}
		
		for(Item item : items) {
			Label lblNewLabel = new Label(composite, SWT.BORDER);
			lblNewLabel.setFont(SWTResourceManager.getFont("微软雅黑", 15, SWT.NORMAL));
			lblNewLabel.setAlignment(SWT.CENTER);
			
			int x = item.getX() * SIZE;
			int y = item.getY() * SIZE;
			int width = item.getWidth() * SIZE;
			int height = item.getHeight() * SIZE;
			
			lblNewLabel.setBounds(x, y, width, height);
			
			lblNewLabel.setText(item.getNum() + "");
			
			// 点击时变成蓝色
			lblNewLabel.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseUp(MouseEvent e) {
					// 清空所有内部label的底色
					for(Control c : composite.getChildren()) {
						c.setBackground(null);
					}
					// 设置当前的label底色为蓝色
					lblNewLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
					clickItem = item;
				}
			});
		}
		
		//composite.layout();
		composite.pack();
	}

}
